package com.ggh.jinjilive.ui.social.near;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.ConvertUtils;
import com.ggh.jinjilive.BaseApplication;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.FragmentNearLiveBinding;
import com.ggh.jinjilive.mobile.social.near.NearLive;
import com.ggh.jinjilive.pagestatus.EmptyCallback;
import com.ggh.jinjilive.pagestatus.LoadingCallback;
import com.ggh.jinjilive.ui.BaseFragment;
import com.ggh.jinjilive.ui.social.near.adapter.NearLiveAdapter;
import com.ggh.jinjilive.views.GridItemDecoration;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadSir;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.MaterialHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class NearLiveFragment extends BaseFragment<FragmentNearLiveBinding> implements OnRefreshListener, OnLoadMoreListener {
    private NearViewModel nearViewModel;
    //附近装直播适配器
    private NearLiveAdapter nearLiveAdapter;

    @Override
    public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseApplication.getInstance().getAppComponent().doInjectionMain(this);
        nearViewModel = new ViewModelProvider(this,viewModelFactory).get(NearViewModel.class);
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        return R.layout.fragment_near_live;
    }

    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        View contentView = binding.getRoot().findViewById(R.id.refreshLayout);
        loadService = LoadSir.getDefault().register(contentView, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadService.showCallback(LoadingCallback.class);
                onRefresh(binding.refreshLayout);
            }
        });
        loadService.showCallback(LoadingCallback.class);
        onRefresh(binding.refreshLayout);
    }


    @Override
    public void initView() {
        super.initView();
        binding.refreshLayout.setRefreshHeader(new MaterialHeader(mContext));
        binding.refreshLayout.setRefreshFooter(new ClassicsFooter(mContext));
        //列表
        nearLiveAdapter = new NearLiveAdapter();
        binding.recyclerView.setLayoutManager(new GridLayoutManager(mContext,2));
        binding.recyclerView.addItemDecoration(new GridItemDecoration(2, ConvertUtils.dp2px(10),true));
        binding.recyclerView.setAdapter(nearLiveAdapter);
    }

    @Override
    public void initData() {
        super.initData();
        //附近直播列表
        nearViewModel.nearData.observe(this, new Observer<List<NearLive>>() {
            @Override
            public void onChanged(List<NearLive> nearLiveList) {
                binding.refreshLayout.finishRefresh();
                if(nearViewModel.isRefresh){
                    if(null != nearLiveList && nearLiveList.size() > 0){
                        binding.refreshLayout.finishLoadMore();
                        nearLiveAdapter.setAdd(nearLiveList);
                        loadService.showSuccess();
                    }else {
                        loadService.showCallback(EmptyCallback.class);
                    }
                }else {
                    if(nearLiveList.size() > 0){
                        nearLiveAdapter.Add(nearLiveList);
                        binding.refreshLayout.finishLoadMore();
                    }else {
                        binding.refreshLayout.finishLoadMoreWithNoMoreData();
                    }
                }
            }
        });
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        //刷新
        binding.refreshLayout.setOnRefreshListener(this);
        //加载
        binding.refreshLayout.setOnLoadMoreListener(this);
    }

    @Override
    public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
        nearViewModel.isRefresh = true;
        nearViewModel.nearbyLive();
    }

    @Override
    public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
        nearViewModel.isRefresh = false;
        nearViewModel.nearbyLive();
    }

}